package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.Showselect;
import com.qf.dao.impl.GoodsDaoImpl;
import com.qf.dao.impl.ShowselectImpl;
import com.qf.domain.Goods;
import com.qf.domain.GoodsType;
import com.qf.domain.PageBean;
import com.qf.domain.User;
import com.qf.service.GoodsService;
import com.qf.service.GoodsTypeService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private Showselect showselect = new ShowselectImpl();

    @Override
    public PageBean<Goods> findByPage(int page_num, int page_size, String where, List<Object> params) {
        long totalSize = goodsDao.getCount(where,params);
        List<Goods> data=goodsDao.selectByPage(page_num,page_size,where,params);
        PageBean<Goods> pageBean = new PageBean<Goods>(page_num,page_size,totalSize,data);
        return pageBean;
    }

    @Override
    public Goods findById(Integer gid) {
        Goods goods= goodsDao.selectById(gid);
        //根据商品类型id,查找商品类型
        if (goods != null) {
            Integer typeid = goods.getTypeid();
            //调用业务层
            GoodsTypeService goodTypeService=new GoodsTypeServiceImpl();
            GoodsType goodsType=goodTypeService.findById(typeid);
            goods.setGoodsType(goodsType);
        }
        return goods;

    }

    @Override
    public void add(Goods goods) {
        goodsDao.insert(goods);
    }

    @Override
    public List<GoodsType> find() {
        return goodsDao.select();
    }

    @Override
    public List<User> selectUser() {
        return goodsDao.selectUser();
    }

    @Override
    public void addgoodType(GoodsType goodsType) {
        goodsDao.add(goodsType);
    }

    @Override
    public List<Goods> selectGoods() {
        return goodsDao.selectgoods();
    }

    @Override
    public void remove(int id) {
        goodsDao.deleteGoods(id);
    }

    @Override
    public List<User> selectinvalidUser() {
        return goodsDao.selectinvalidUser();
    }

    @Override
    public void update(Goods goods) {
        goodsDao.update(goods);
    }

    @Override
    public List<Goods> findAll() {
        List<Goods> goods = goodsDao.findAll();
        for (Goods good : goods) {
            GoodsType goodsType = showselect.selectgoodById(good.getTypeid());
            good.setGoodsType(goodsType);
        }
        return goods;
    }
}
